﻿using System;
using System.Net;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace HasuSLLib.Utility
{
    public class CustomAttribute
    {

        #region object atribute

        public static bool HasAttribute<T>(object entity, out T tout) where T : Attribute
        {
            return HasAttribute<T>(entity, out  tout, true);
        }
        public static bool HasAttribute<T>(object entity, out T tout, bool inherit) where T : Attribute
        {
            T[] t = GetAttributes<T>(entity, inherit);
            if (t.Length > 0)
            {
                tout = t[0];
                return true;
            }
            else
            {
                tout = null;
                return false;
            }
        }


        public static bool HasAttributes<T>(object entity, out T[] tout) where T : Attribute
        {

            return HasAttributes<T>(entity, out  tout, true);
        }
        public static bool HasAttributes<T>(object entity, out T[] tout, bool inherit) where T : Attribute
        {
            tout = GetAttributes<T>(entity, inherit);
            if (tout.Length > 0)
                return true;
            else
                return false;
        }


        #endregion


        #region get object attribute

        public static T GetAttribute<T>(object entity) where T : Attribute
        {
            T[] t = GetAttributes<T>(entity, true);
            if (t.Length > 0)
                return t[0];
            else
                return null;
        }


        public static T[] GetAttributes<T>(object entity, bool inherit) where T : Attribute
        {
            T[] t = (T[])entity.GetType().GetCustomAttributes(typeof(T), inherit);

            return t;
        }

        #endregion



        #region metadata atribute

        public static bool HasAttribute<T>(PropertyInfo metadata, out T tout) where T : Attribute
        {

            return HasAttribute<T>(metadata, out  tout, true);
        }
        public static bool HasAttribute<T>(PropertyInfo metadata, out T tout, bool inherit) where T : Attribute
        {
            T[] t = GetAttributes<T>(metadata, inherit);
            if (t.Length > 0)
            {
                tout = t[0];
                return true;
            }
            else
            {
                tout = null;
                return false;
            }
        }


        public static bool HasAttributes<T>(PropertyInfo metadata, out T[] tout) where T : Attribute
        {

            return HasAttributes<T>(metadata, out  tout, true);
        }
        public static bool HasAttributes<T>(PropertyInfo metadata, out T[] tout, bool inherit) where T : Attribute
        {
            tout = GetAttributes<T>(metadata, inherit);
            if (tout.Length > 0)
                return true;
            else
                return false;
        }

        public static bool HasAttribute<T>(PropertyInfo metadata) where T : Attribute
        {
            T[] t = GetAttributes<T>(metadata, true);
            if (t.Length > 0)
                return true;
            else
                return false;
        }

        #endregion


        #region getattribute

        public static T GetAttribute<T>(PropertyInfo metadata) where T : Attribute
        {
            T[] t = GetAttributes<T>(metadata, true);
            if (t.Length > 0)
                return t[0];
            else
                return null;
        }


        public static T[] GetAttributes<T>(PropertyInfo metadata, bool inherit) where T : Attribute
        {
            T[] t = (T[])metadata.GetCustomAttributes(typeof(T), inherit);

            return t;
        }

        #endregion

    }

}
